package jrobutt.room.menu;

import jrobutt.*;

import java.awt.*;
import java.awt.event.*;

public class MenuItem implements KeyboardResponder, Renderable
{
    public static final int RIGHT_KEY = KeyEvent.VK_RIGHT;
    public static final int LEFT_KEY = KeyEvent.VK_LEFT;
    public static final int DOWN_KEY = KeyEvent.VK_DOWN;
    public static final int UP_KEY = KeyEvent.VK_UP;
    
    public float x;
    public float y;
    
    public boolean is_selected;
    
    public GameSprite selected_sprite;
    public GameSprite unselected_sprite;
    
    public MenuItem right;
    public MenuItem left;
    public MenuItem down;
    public MenuItem up;
    
    private GameSprite get_current_sprite ()
    {
        return is_selected ? selected_sprite : unselected_sprite;
    }
    
    public MenuItem (
        float x, float y, boolean is_selected, GameSprite selected_sprite, GameSprite unselected_sprite,
        MenuItem right, MenuItem left, MenuItem down, MenuItem up)
    {
        this.x = x;
        this.y = y;
        
        this.is_selected = is_selected;
        
        this.selected_sprite = selected_sprite;
        this.unselected_sprite = unselected_sprite;
        
        this.right = right;
        this.left = left;
        this.down = down;
        this.up = up;
    }
    
    private void select ()
    {
        is_selected = true;
    }
    private void unselect ()
    {
        is_selected = false;
    }
    
    public void key_pressed (int key)
    {
        if (!is_selected) return;
        
        MenuItem next = null;
        if (key == RIGHT_KEY)
        {
            next = right;
        }
        else if (key == LEFT_KEY)
        {
            next = left;
        }
        else if (key == DOWN_KEY)
        {
            next = down;
        }
        else if (key == UP_KEY)
        {
            next = up;
        }
        
        if (next != null)
        {
            this.unselect();
            next.select();
        }
    }
    public void key_released (int key) {}
    
    public void render (Graphics2D g)
    {
        get_current_sprite().renderAt(g, x, y);
    }
}
